﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using Microsoft.Practices.Unity;
using Microsoft.Surface.Presentation.Controls;
using ScrumTable.UI.View.Util;
using ScrumTable.UI.ViewModel.Tools;
namespace ScrumTable.UI.View.WinTouch.UserControls.Tool
{
	/// <summary>
	/// Interaction logic for SearchScreen.xaml
	/// </summary>
	public partial class ReportControl
    {

        #region Decleration
        //--------------------------------------------------------------------
        // Decleration
        //--------------------------------------------------------------------
        public Dictionary<string, object> SelectedValues { get; set; }
        private ScatterViewItem _selectorItem;
        private readonly ClickUtil _clickReportUtilTouch = new ClickUtil();
        private readonly ClickUtil _clickReportUtil = new ClickUtil();
        private readonly DispatcherTimer _reloadTimer = new DispatcherTimer();
     

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
   
        [Dependency]
        public ViewModelReportControl Model
        {
            set
            {
                DataContext = value;
            }
            get
            {
                return (ViewModelReportControl)DataContext;
            }

        }


      
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        public ReportControl()
        {
            InitializeComponent();
            Loaded += OnLoaded;
            _reloadTimer.Interval = TimeSpan.FromMinutes(30);
            _reloadTimer.Tick += OnReloadTimerTick;
        }

     

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------


        protected override void ReleaseManagedResources()
        {
            if (_selectorItem != null)
            {
                DisposeItem(ViewElementUtils.FindVisualChild<WpfReportSelector>(_selectorItem).Model);
            }
            _reloadTimer.Stop();
            base.ReleaseManagedResources();
        }
        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            Loaded -= OnLoaded;
            _reloadTimer.Start();
        }

        private void OnSelectClick(object sender, RoutedEventArgs e)
        {
            if (_selectorItem != null)
            {
                DisposeItem(ViewElementUtils.FindVisualChild<WpfReportSelector>(_selectorItem).Model);
            }
            var selector = Model.Container.Resolve<WpfReportSelector>();
            selector.IsSelectMode = true;
            _selectorItem = Model.Container.Resolve<AppScreen>().AddScatteredItem(selector, Application.Current.MainWindow.PointToScreen(new Point(Application.Current.MainWindow.Width/2,Application.Current.MainWindow.Height/2)), true,true, Model.Name, false);
            selector.Model.ReportSelected += OnReportSelected;
            _selectorItem.Unloaded += OnItemUnloaded;
        }


        void OnReloadTimerTick(object sender, EventArgs e)
        {
            if (Model.SelectedReport != null)
            {
                Model.SelectedReport = Model.SelectedReport;
            }
        }

        private void OnItemUnloaded(object sender, RoutedEventArgs e)
        {
            if (_selectorItem != null)
            {
                DisposeItem(ViewElementUtils.FindVisualChild<WpfReportSelector>(_selectorItem).Model);
            }
        }

        private void DisposeItem(ViewModelReportSelector reportSelector)
	    {
	        Model.Container.Resolve<AppScreen>().RemoveScattertItem(_selectorItem);
	        reportSelector.ReportSelected -= OnReportSelected;
            _selectorItem.Unloaded -= OnItemUnloaded;
	        _selectorItem = null;
	    }

	    private void OnDeleteClick(object sender, RoutedEventArgs e)
        {
            Model.SelectedReport = null;
        }


        private void OnReportSelected(object sender, ReportSelectEventArgs e)
        {
            if (_selectorItem != null)
            {
                Model.SelectedReport = ElementManager.GetReport(e.Report.Name);
            }
        }


        private void OnReportClicked(object sender, TouchEventArgs e)
        {
            e.Handled = ShowImageScatterItem(e, true);
        }

        private void OnReportClicked(object sender, MouseButtonEventArgs e)
        {
            e.Handled = ShowImageScatterItem(e, false);
        }

        private bool ShowImageScatterItem(InputEventArgs args, bool isTouch)
        {
            if (PresentationSource.FromVisual(this) == null)
            {
                return false;
            }

            if ((isTouch ? !_clickReportUtilTouch.IsClickInTimeAndPlace(args) : !_clickReportUtil.IsClickInTimeAndPlace(args)))
                return false;

            var window = (AppScreen)Application.Current.MainWindow;
            window.AddReportImageToScatterItem(Model.SelectedReportUri, Model.SelectedReportPost );
            return true;
        } 

    

        #endregion

      
    }
}